package com.dd.cloud.common.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 地址工具类
 *
 * @author Lx
 * @version 1.0
 */
public class AddrUtils {
    /**
     * 省市区正则
     */
    private static final Pattern PROVINCE_CITY_DISTRICT_PATTERN = Pattern.compile("(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)(?<district>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛|.+镇|.+街道|.+委会|.+农场|.+开发区|.+街)(?<addr>.*)");

    /**
     * 获取省份
     *
     * @param str
     * @return
     */
    public static String getProvince(String str) {
        Matcher matcher = PROVINCE_CITY_DISTRICT_PATTERN.matcher(str);
        if (matcher.find()) {
            return matcher.group("province");
        }
        return "";
    }

    /**
     * 获取城市
     *
     * @param str
     * @return
     */
    public static String getCity(String str) {
        Matcher matcher = PROVINCE_CITY_DISTRICT_PATTERN.matcher(str);
        if (matcher.find()) {
            return matcher.group("city");
        }
        return "";
    }

    /**
     * 获取区县
     *
     * @param str
     * @return
     */
    public static String getDistrict(String str) {
        Matcher matcher = PROVINCE_CITY_DISTRICT_PATTERN.matcher(str);
        if (matcher.find()) {
            return matcher.group("district");
        }
        return "";
    }

    public static AddrEntity getAddrEntity(String str) {
        AddrEntity addrEntity = new AddrEntity();
        Matcher matcher = PROVINCE_CITY_DISTRICT_PATTERN.matcher(str);
        if (matcher.find()) {
            addrEntity.setProvince(matcher.group("province"));
            addrEntity.setCity(matcher.group("city"));
            addrEntity.setDistrict(matcher.group("district"));
            addrEntity.setAddr(matcher.group("addr"));
        } else {
            addrEntity.setFlag(0);
        }
        return addrEntity;
    }

    /**
     * 验证省市区正则
     *
     * @param str
     * @return
     */
    public static boolean verifyProvinceCityDistrict(String str) {
        if (StringUtils.isBlank(str)) {
            return false;
        }
        Matcher matcher = PROVINCE_CITY_DISTRICT_PATTERN.matcher(str);
        return matcher.matches();
    }

    /**
     * 地址属性类
     *
     * @author Lx
     * @version 1.0
     */
    public static class AddrEntity {
        /**
         * 省份
         */
        private String province;
        /**
         * 城市
         */
        private String city;
        /**
         * 区县
         */
        private String district;
        /**
         * 详细地址
         */
        private String addr;
        /**
         * 是否有地址信息 1是 0否
         */
        private Integer flag = 1;

        public String getProvince() {
            return province;
        }

        public void setProvince(String province) {
            this.province = province;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getDistrict() {
            return district;
        }

        public void setDistrict(String district) {
            this.district = district;
        }

        public String getAddr() {
            return addr;
        }

        public void setAddr(String addr) {
            this.addr = addr;
        }

        public Integer getFlag() {
            return flag;
        }

        public void setFlag(Integer flag) {
            this.flag = flag;
        }

        @Override
        public String toString() {
            return "AddrEntity{" +
                    "province='" + province + '\'' +
                    ", city='" + city + '\'' +
                    ", district='" + district + '\'' +
                    ", addr='" + addr + '\'' +
                    ", flag=" + flag +
                    '}';
        }
    }
}
